home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 0294.ZIP / INTEREST.C < prev    next >
C/C++ Source or Header  |  1985-05-18  |  2KB  |  101 lines

  1. /*
  2. amount = starting balance
  3. balance = remaining balance
  4. rate = interest rate per period
  5. rate1 = interest rate per year
  6. payment = monthly payment
  7. principal = payment to principle
  8. interest = payment of interest per period
  9.  
  10. *******************************************************/
  11. /*        declare variables        */
  12.  
  13.     float amount;
  14.     float balance =0;
  15.     float rate1;
  16.     float rate;
  17.     float payment;
  18.     float principal = 0;
  19.     float interest = 0;
  20.  
  21. /*    **************************    */
  22. #include "stdio.h"
  23.         FILE *outfile, *fopen(), *fclose();
  24.  
  25. interest1()    /* call compute and print results    */
  26.  
  27. {
  28.         static char out_name[]="amortize.txt";
  29.  
  30.         outfile = fopen(out_name,"w");
  31.             if(outfile == NULL)
  32.                 printf("cant open amortize.txt\n");
  33.     
  34.         balance = amount;
  35.         int x =0;
  36.  
  37.         printf(" payment   interest   principal   balance\n");
  38.         fprintf(outfile," payment   interest   principal   balance\n");
  39.  
  40.     while (balance >0)    /* call compute and print results */
  41.         {
  42.         compute();
  43.         printf("%8.2f  %8.2f  %8.2f    %8.2f   %d\n",payment, interest,
  44.                                              principal, balance, ++x);
  45.         fprintf(outfile,"%8.2f  %8.2f  %8.2f    %8.2f   %d\n",payment, interest,
  46.                                              principal, balance,x);
  47.         line();
  48.         
  49.             if (x%12 ==0)    /* pause every 12 payments  */
  50.                 {
  51.                 printf("press return ");
  52.                 getchar();
  53.                 }
  54.  
  55.         }
  56.  
  57.     fclose(outfile);
  58.     printf("Done. The results have been saved in a file named %s\n",out_name);
  59.  
  60. }
  61.  
  62.  
  63. /*        compute balance        */
  64.     compute()
  65.         {
  66.  
  67.         if (balance > payment)
  68.             {
  69.             interest = balance * rate;
  70.             principal = payment - interest;
  71.             balance = balance - principal;
  72.             return (interest);
  73.             return (principal);
  74.             return (balance);
  75.             }
  76.         else
  77.             {
  78.             interest = balance * rate;
  79.             payment = balance + interest;
  80.             principal = payment - interest;
  81.             balance = balance - principal;
  82.             return (interest);
  83.             return (payment);
  84.             return (principal);
  85.             return (balance);
  86.             }
  87.         }
  88.  
  89.  
  90. line()
  91.     {
  92.     int x = 0;
  93.         while (x++<=78)
  94.             {
  95.             printf("-");
  96.             fprintf(outfile,"-");
  97.             }
  98.     putchar('\n');
  99. fprintf(outfile,"\n");
  100.     }